﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using LGSVR.BLL.Terminal;
using LGSVR.BLL.Sys;
using System.Data;

namespace LGSVR_WEB.terminal
{
    public partial class ConfigInfoEdit : DataBasePage
    {
        private int Pid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["ID"]) ? 0 : int.Parse(Request.QueryString["ID"]);
            }
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }
        private void PageInit()
        {
            ParamList parm = new ParamList();
           
            dropScanType.DataSource = parm.GetListByBelong("SCAN_PARAM");
            dropScanType.DataTextField = "typename";
            dropScanType.DataValueField = "ID";
            dropScanType.DataBind();
            dropScanType.Items.Insert(0, new ListItem("-请选择-", ""));

           
            dropactCount.Items.Add(new ListItem("127","127"));
            dropactCount.Items.Add(new ListItem("63", "63"));
            dropactCount.Items.Add(new ListItem("31", "31"));
            dropactCount.Items.Insert(0, new ListItem("-请选择-", ""));

            dropdefFontName.DataSource = parm.GetListByBelong("DEF_FONT_NAME");
            dropdefFontName.DataTextField = "typename";
            dropdefFontName.DataValueField = "ID";
            dropdefFontName.DataBind();
            dropdefFontName.Items.Insert(0, new ListItem("-请选择-", ""));

            dropdefAlignW.DataSource = parm.GetListByBelong("DEF_W_ALIGN");
            dropdefAlignW.DataTextField = "typename";
            dropdefAlignW.DataValueField = "ID";
            dropdefAlignW.DataBind();
            dropdefAlignW.Items.Insert(0, new ListItem("-请选择-", ""));

            dropdefAlignH.DataSource = parm.GetListByBelong("DEF_H_ALIGN");
            dropdefAlignH.DataTextField = "typename";
            dropdefAlignH.DataValueField = "ID";
            dropdefAlignH.DataBind();
            dropdefAlignH.Items.Insert(0,new ListItem("-请选择-", ""));

            Dictionary<string, string> dicSingle;
            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Pid != 0)
                {
                    dicSingle = GetSigle("configInfo", "ID=" + Pid);
                    if (dicSingle != null)
                    {
                        txtcontent.Text = dicSingle["content"];
                        txtconfigID.Text = dicSingle["configID"];
                        dropScanType.SelectedValue = dicSingle["scanType"];
                        chkOE.Checked = Utils.ToBool(dicSingle["OE"]);
                        chkDataType.Checked = Utils.ToBool(dicSingle["dataType"]);
                        rbtIsDoubleColor.SelectedValue = dicSingle["isDoubleColor"];
                        txtsWidth.Text = dicSingle["sWidth"];
                        txtsHeight.Text = dicSingle["sHeight"];
                        dropactCount.SelectedValue = dicSingle["actCount"];
                        chksmnx.Checked = Utils.ToBool(dicSingle["smnx"]);
                        chksjnx.Checked = Utils.ToBool(dicSingle["sjnx"]);
                        chkzhsc.Checked = Utils.ToBool(dicSingle["zhsc"]);
                        chkyxbj.Checked = Utils.ToBool(dicSingle["yxbj"]);
                        chkjmcf.Checked = Utils.ToBool(dicSingle["jmcf"]);
                        chkjscb.Checked = Utils.ToBool(dicSingle["jscb"]);
                        chkzctb.Checked = Utils.ToBool(dicSingle["zctb"]);
                        chkzcgm.Checked = Utils.ToBool(dicSingle["zcgm"]);
                        txtAPN.Text = dicSingle["APN"];
                        txtserverIP.Text = dicSingle["serverIP"];
                        txtportID.Text = dicSingle["portID"];
                        txtdefWCount.Text = dicSingle["defWCount"];
                        txtdefHCount.Text = dicSingle["defHCount"];
                        dropdefFontName.SelectedValue = dicSingle["defFontName"];
                        txtdefFontSize.Text = dicSingle["defFontSize"];
                        txtdelFontStyle.Text = dicSingle["delFontStyle"];
                        dropdefAlignW.SelectedValue = dicSingle["defAlignW"];
                        dropdefAlignH.SelectedValue = dicSingle["defAlignH"];
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            ConfigInfoBLL cardTypeBll = new ConfigInfoBLL();
            //if (cardTypeBll.IsHasConfigID(txtconfigID.Text.Trim(), ConfigID))
            //{
            //    MsgContent = ShowMessage("已存在配置编号，请更换！", MessageType.Error, false, true);
            //    return;
            //}

            if (txtconfigID.Text.Trim() == "")
            {
                MsgContent = ShowMessage("配置编号不能为空！", MessageType.Error, false, true);
                return;
            }
            if (HasKey("configInfo", "configID", txtconfigID.Text.Trim(), Pid))
            {
                MsgContent = ShowMessage("已存在配置编号，请更换！", MessageType.Error, false, true);
                return;
            }

            Dictionary<string, string> dicParam = new Dictionary<string, string>();
            dicParam.Add("configID", txtconfigID.Text.Trim());


            dicParam.Add("scanType", dropScanType.SelectedValue);
            dicParam.Add("OE", chkOE.Checked ? "1" : "0");
            dicParam.Add("dataType", chkDataType.Checked ? "1" : "0");
            dicParam.Add("isDoubleColor", rbtIsDoubleColor.SelectedValue);
            dicParam.Add("sWidth", txtsWidth.Text);
            dicParam.Add("sHeight", txtsHeight.Text);
            dicParam.Add("actCount", dropactCount.SelectedValue);
            dicParam.Add("smnx", chksmnx.Checked ? "1" : "0");
            dicParam.Add("sjnx", chksjnx.Checked ? "1" : "0");
            dicParam.Add("zhsc", chkzhsc.Checked ? "1" : "0");
            dicParam.Add("yxbj", chkyxbj.Checked ? "1" : "0");
            dicParam.Add("jmcf", chkjmcf.Checked ? "1" : "0");
            dicParam.Add("jscb", chkjscb.Checked ? "1" : "0");
            dicParam.Add("zctb", chkzctb.Checked ? "1" : "0");
            dicParam.Add("zcgm", chkzcgm.Checked ? "1" : "0");

            dicParam.Add("APN", txtAPN.Text.Trim());
            dicParam.Add("serverIP", txtserverIP.Text.Trim());
            dicParam.Add("portID", txtportID.Text.Trim());

            dicParam.Add("defWCount", txtdefWCount.Text);
            dicParam.Add("defHCount", txtdefHCount.Text);
            dicParam.Add("content", txtcontent.Text);
            dicParam.Add("defFontName", dropdefFontName.SelectedValue);
            dicParam.Add("defFontSize", txtdefFontSize.Text);
            dicParam.Add("delFontStyle", txtdelFontStyle.Text);
            dicParam.Add("defAlignW", dropdefAlignW.SelectedValue);
            dicParam.Add("defAlignH", dropdefAlignH.SelectedValue);
            dicParam.Add("state", "1");




            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (Pid != 0)
                {
                    Edit("configInfo", dicParam, "ID=" + Pid);

                    if (Result > 0)
                    {
                        MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                    else
                    {
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    }
                }
            }
            else  // 新增
            {
                int id = Insert("configInfo", dicParam);
                if (Result > 0)
                {
                    MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                    NoRefresh();
                }
                else
                {
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                }
            }
        }
        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("ConfigInfoList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("ConfigInfoList.aspx", false);
            }
        }
    }
      
}